package com.qf.fmall.shopcart.controller;

import com.qf.fmall.shopcart.entity.ShoppingCart;
import com.qf.fmall.shopcart.service.IShoppingCartService;
import com.qf.fmall.shopcart.vo.ShopcartListResponseVo;
import com.qf.fmall.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author djl
 * @since 2024-04-28
 */
@CrossOrigin
@RestController
@RequestMapping("/shopcart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private IShoppingCartService shoppingCartService;

    @GetMapping("/listbycids")
    public ResultVo listbycids(Integer[] cids){

        //1. 打印日志
        log.info("接收到的参数为：cids={}", Arrays.asList(cids));

        //2. 校验参数
        if (StringUtils.isEmpty(cids)){
            throw new RuntimeException("购物车id不能为空");
        }

        // 转换参数类型为 Integer[]
//        String[] split = cids.split(",");
//        Integer[] cidArr = new Integer[split.length];
//        for (int i = 0; i < split.length; i++) {
//            cidArr[i] = Integer.valueOf(split[i]);
//        }

        //3. 调用servcie
        List<ShopcartListResponseVo> data = shoppingCartService.listbycids(cids);

        //4. 返回json
        return new ResultVo(10000,"success",data);

    }

    @PutMapping("/update/{cartid}/{number}")
    public ResultVo update(@PathVariable("cartid") Integer cartid,@PathVariable("number") Integer number){

        //1. 打印日志
        log.info("受到的参数为,cartid={},number={}",cartid,number);
        //2. 校验参数
        if (cartid==null||cartid<=0){
            throw new RuntimeException("cartid参数非法");
        }
        if (number==null||number<=0){
            throw new RuntimeException("number参数非法");
        }
        //3. 调用service方法
//        ShoppingCart shoppingCart = new ShoppingCart();
//        shoppingCart.setCartId(cartid);
//        shoppingCart.setCartNum(number+"");
//        shoppingCartService.updateById(shoppingCart);

        shoppingCartService.updateByIdBysql(cartid,number);

        return new ResultVo(10000,"success",null);
    }

    @GetMapping("/list")
    public ResultVo list(@RequestParam("userId") Integer id){

        //1. 打印日志
        log.info("接收到的参数为,userId={}",id);

        //2. 校验参数
        if (id==null|| id<=0){
            throw new RuntimeException("参数id非法");
        }

        //3. 调用service进行业务处理
        List<ShopcartListResponseVo> data =  shoppingCartService.listByUserId(id);

        //4. 返回json
        return new ResultVo(10000,"success",data);
    }


    @PostMapping("/add")
    public ResultVo add(@RequestBody @Valid ShoppingCart shoppingCart){

        //1. 打印日志
        log.info("接收到的参数为:shoppingCart={}",shoppingCart);
        //2. 参数校验
        checkShopCart(shoppingCart);
        //3. 调用servcie方法进行业务处理
        // 补全缺失的数据
        // 获取当前时间的字符串形式, yyyy-MM-dd HH:mm:ss
        String dateTimeStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        shoppingCart.setCartTime(dateTimeStr);
//        shoppingCart.setCartTime(new Date());
        shoppingCartService.save(shoppingCart);

        // 返回json
        return new ResultVo(10000,"successs",null);

    }

    private void checkShopCart(ShoppingCart shoppingCart) {
        String cartNum = shoppingCart.getCartNum();
        Integer cartIntNum = Integer.valueOf(cartNum);
        if (cartIntNum<=0){
            throw new RuntimeException("购物车数量参数非法，不能小于0");
        }
    }


}
